function [free_entry] = M_q_zero(scale_Mq,inputs_Mq_struc)

% Takes a bunch of parameters and the scaling factor 'scale_Mq' as inputs
% and returns 'free_entry' which is the NPV of profits which needs to be set to 0.
% The scaling factors scale a guess of M_q_approx to yield M_q. This scaling factor is then
% adjusted (which in effect adjusts the M_q), till the free entry conditons
% hold i.e. free_entry=0.
eta               = inputs_Mq_struc.eta;               
sigma             = inputs_Mq_struc.sigma;
w_U               = inputs_Mq_struc.w_U;             
w_S_target        = inputs_Mq_struc.w_S;        
N                 = inputs_Mq_struc.N;       
h                 = inputs_Mq_struc.h;                 
entry_sh_S_q        = inputs_Mq_struc.entry_sh_S_q;
P_q_norm          = inputs_Mq_struc.P_q_norm;          
q                 = inputs_Mq_struc.q;                 
a_q               = inputs_Mq_struc.a_q;               
f_q               = inputs_Mq_struc.f_q;               
pdf_A_q_i         = inputs_Mq_struc.pdf_A_q_i;         
u_fn              = inputs_Mq_struc.u_fn; 
prod_grid_size    = inputs_Mq_struc.prod_grid_size; 
p_q_i    = inputs_Mq_struc.p_q_i; 
MC_L_q_i    = inputs_Mq_struc.MC_L_q_i; 
M_q_approx    = inputs_Mq_struc.M_q_approx; 
delta    = inputs_Mq_struc.delta;
beta    = inputs_Mq_struc.beta;
pi_old    = inputs_Mq_struc.pi_old;

M_q = M_q_approx.*scale_Mq;

P_q = P_q_norm.*(M_q.^(eta - 1/(sigma-1)));

% Share of unskilled and skilled who buy different qualities
u_q_U = u_fn(q,P_q,w_U,a_q,pi_old);
rho_q_U = exp(u_q_U)./(sum(exp(u_q_U)));
u_q_S = u_fn(q,P_q,w_S_target,a_q,pi_old);
rho_q_S = exp(u_q_S)./(sum(exp(u_q_S)));

% Total Demand for a particular quality. 
D_q = N*(1-h)*rho_q_U.*((w_U+pi_old)./P_q) + N*h*rho_q_S.*((w_S_target+pi_old)./P_q);


d_q_i_temp = M_q.^(eta - sigma/(sigma-1)).*D_q.*(P_q_norm.^sigma);
d_q_i  = repmat(d_q_i_temp,1,prod_grid_size)./(p_q_i.^sigma);

% Free Entry
entry_cost_q = (f_q*w_U.*(1-entry_sh_S_q) + f_q*w_S_target.*entry_sh_S_q);
profits_npv = 1/(1-beta*(1-delta))*sum((p_q_i - MC_L_q_i).*d_q_i.*pdf_A_q_i,2);
free_entry = log(profits_npv./entry_cost_q);

end


